package com.wy.controller;

import com.wy.elasticsearch.BaseInfo;
import com.wy.elasticsearch.ElasticSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
public class BaseInfoController {
    @Autowired
    private ElasticSearchService elasticSearchService;

    @GetMapping("base/getall")
    public List<BaseInfo> getAll(){
        return elasticSearchService.findAll();
    }

    @GetMapping("base/save")
    public String save(){
        elasticSearchService.save();

        return "succ";
    }

    @GetMapping("base/fuzzy")
    public Page<BaseInfo> fuzzFind(String name){
        return elasticSearchService.testFuzzyQuery(name);

    }

    @GetMapping("base/startWith")
    public Page<BaseInfo> startWith(String regionCode){
        return elasticSearchService.startWith(regionCode);

    }
    @GetMapping("base/termQuery")
    public Page<BaseInfo> termQuery(String regionCode){
        return elasticSearchService.termQuery(regionCode);

    }
    @GetMapping("base/range")
    public Page<BaseInfo> rangeQuery(String start,String end) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return elasticSearchService.rangeQuery(sdf.parse(start),sdf.parse(end));

    }

    @GetMapping("base/multi")
    public Page<BaseInfo> multiQuery() throws Exception{
        return elasticSearchService.multiConditionalQuery();

    }

    @GetMapping("base/deleteIndex")
    public String deleteIndex(){
        elasticSearchService.deleteIndex();
        return "succ";

    }





}
